package com.network.web.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Objects;

/**
 * @author 28162
 */
@Slf4j
public class FileUtils {
    public static FileInputStream fileToInputStreamByPath(String relativePath) {
        FileInputStream fileInputStream = null;

        // 使用 ClassLoader 获取 test.pfx 文件的绝对路径
        URL resourceUrl = FileUtils.class.getResource(relativePath);
        // 从 URL 转换为 File 对象
        if (!Objects.isNull(resourceUrl)) {
            File file = new File(resourceUrl.getFile());
            try {
                fileInputStream = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                log.error("根据文件路径创建FileInputStream失败, 导致创建失败的文件路径为: {}", relativePath, e);
                return fileInputStream;
            }
        }

        return fileInputStream;
    }
}
